#coding:utf-8

from flask import Flask, g
from RedisClient import RedisClient

'''
    作用：外部接口模块，可以由外部请求获取代理值
'''

__all__ = ['app']
app = Flask(__name__)

def get_conn():
    if not hasattr(g, 'redis'):
        g.redis = RedisClient()
    return g.redis

@app.route('/')
def index():
    return '<h2>welcome to Proxy Pool System</h2>'

@app.route('/random')
def get_proxy():
    '''
    获取随机可用代理
    '''
    conn = get_conn()
    return conn.random()

@app.route('/count')
def get_counts():
    '''
    获取代理池总数量
    '''
    conn = get_conn()
    return str(conn.count())

if __name__ == '__main__':
    app.run()
